//<<<+++OPENSOURCE
//<<<+++OPENSOURCE_LICENSE
/**
 * @addtogroup FPDFAPI
 * @{
 */

/**
 * @file
 * @brief PDF appearance referred classes.
 */

#ifndef _FPDF_AP_H_
#define _FPDF_AP_H_

#ifndef _FX_BASIC_H_
#include "../fxcrt/fx_basic.h"
#endif

#ifndef _FPDF_PARSER_
#include "../fpdfapi/fpdf_parser.h"
#endif

#ifndef _FPDF_VT_H_
#include "fpdf_vt.h"
#endif

/** @brief Variable text font map interface. */
class IPVT_FontMap
{
public:
	/**
	 * Get a font.
	 *
	 * @param[in] nFontIndex			The zero-based font index.
	 * @return A font.
	 */
	virtual CPDF_Font*						GetPDFFont(FX_INT32 nFontIndex) = 0;
	/**
	 * Get the alias of a font.
	 *
	 * @param[in] nFontIndex			The zero-based font index.
	 * @return The alias of the font.
	 */
	virtual CFX_ByteString					GetPDFFontAlias(FX_INT32 nFontIndex) = 0;
};

/** @brief Variable text dash structure. */
struct CPVT_Dash
{
	/** The constructor with parameters. */
	CPVT_Dash(FX_INT32 dash, FX_INT32 gap, FX_INT32 phase) : nDash(dash), nGap(gap), nPhase(phase)
	{}

	/** The dash length. */
	FX_INT32			nDash;
	/** The gap length. */
	FX_INT32			nGap;
	/** The dash phase distance. */
	FX_INT32			nPhase;
};

/**
 * @name Color type for variable text.
 */
/*@{*/

/** @brief Transparent. */
#define CT_TRANSPARENT		0
/** @brief Gray. */
#define	CT_GRAY				1
/** @brief RGB. */
#define	CT_RGB				2
/** @brief CMYK. */
#define	CT_CMYK				3

/*@}*/

/** @brief Variable text color structure. */
struct CPVT_Color
{
	/** Construct with parameters. */
	CPVT_Color(FX_INT32 type = 0, FX_FLOAT color1 = 0.0f, FX_FLOAT color2 = 0.0f, FX_FLOAT color3 = 0.0f, FX_FLOAT color4 = 0.0f)
		: nColorType(type), fColor1(color1), fColor2(color2), fColor3(color3), fColor4(color4)
	{}

	/** The color type. 0:transparent, 1:gray, 2:rgb, 3:cmyk. */
	FX_INT32			nColorType;

	FX_FLOAT			fColor1;	/**< The first component. */
	FX_FLOAT			fColor2;	/**< The second component. */
	FX_FLOAT			fColor3;	/**< The third component. */
	FX_FLOAT			fColor4;	/**< The fourth component. */
};

/** @brief Variable text provider class. */
class CPVT_Provider : public IPDF_VariableText_Provider
{
public:
	/**
	 * Construct with a variable text font map.
	 *
	 * @param[in] pFontMap		The input variable text font map.
	 */
	CPVT_Provider(IPVT_FontMap * pFontMap);
	/** The destructor. */
	virtual ~CPVT_Provider();

	/**
	 * Get a character width.
	 *
	 * @param[in] nFontIndex		The zero-based font index.
	 * @param[in] word				The character code.
	 * @param[in] nWordStyle		The word style.
	 * @return The character width.
	 */
	FX_INT32						GetCharWidth(FX_INT32 nFontIndex,FX_WORD word, FX_INT32 nWordStyle);
	/**
	 * Get the font ascent.
	 *
	 * @param[in] nFontIndex		The zero-based font index.
	 * @return The font ascent.
	 */
	FX_INT32						GetTypeAscent(FX_INT32 nFontIndex);
	/**
	 * Get the font descent.
	 *
	 * @param[in] nFontIndex		The zero-based font index.
	 * @return The font descent.
	 */
	FX_INT32						GetTypeDescent(FX_INT32 nFontIndex);
	/**
	 * Get the word index in font with specified charset.
	 *
	 * @param[in] word				The input character code.
	 * @param[in] charset			The character set.
	 * @param[in] nFontIndex		The font index.
	 * @return The word index in the font.
	 */
	FX_INT32						GetWordFontIndex(FX_WORD word, FX_INT32 charset, FX_INT32 nFontIndex);
	/**
	 * Check whether the word is a Latin character.
	 *
	 * @param[in] word				The input character code.
	 * @return Non-zero means it's a Latin character.
	 */
	FX_BOOL							IsLatinWord(FX_WORD word);
	/** Get the default font index. */
	FX_INT32						GetDefaultFontIndex();

private:
	/** The variable text font map. */
	IPVT_FontMap *	m_pFontMap;
};

/**
 * @name Border style.
 */
/*@{*/

/** @brief A solid rectangle surrounding the annotation. */
#define PBS_SOLID			0
/** @brief A dashed rectangle surrounding the annotation.  */
#define PBS_DASH			1
/** @brief A simulated embossed rectangle that appears to be raised above the surface of the page. */
#define PBS_BEVELED			2
/** @brief A simulated engraved rectangle that appears to be recessed below the surface of the page.  */
#define PBS_INSET			3
/** @brief A single line along the bottom of the annotation rectangle. */
#define PBS_UNDERLINED		4

/*@}*/

/** @brief PDF AP (appearance stream) generator. */
class CPVT_GenerateAP
{
public:
	/**
	 * Generate the appearance stream for a text field.
	 *
	 * @param[in] pDoc				The PDF document.
	 * @param[in,out] pAnnotDict	Input a text field dictionary and fill the AP entry.
	 * @return Non-zero means success, otherwise failure.
	 */
	static FX_BOOL							GenerateTextFieldAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict);
	/**
	 * Generate the appearance stream for a combo box.
	 *
	 * @param[in] pDoc				The PDF document.
	 * @param[in,out] pAnnotDict	Input a combo box dictionary and fill the AP entry.
	 * @return Non-zero means success, otherwise failure.
	 */
	static FX_BOOL							GenerateComboBoxAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict);
	/**
	 * Generate the appearance stream for a list box.
	 *
	 * @param[in] pDoc				The PDF document.
	 * @param[in,out] pAnnotDict	Input a list box dictionary and fill the AP entry.
	 */
	static FX_BOOL							GenerateListBoxAP(CPDF_Document* pDoc, CPDF_Dictionary* pAnnotDict);
	/**
	 * Generate an edit appearance stream.		
	 */
	static CFX_ByteString					GenerateEditAP(IPVT_FontMap * pFontMap, IPDF_VariableText_Iterator * pIterator, 
												const CPDF_Point & ptOffset, FX_BOOL bContinuous,FX_WORD SubWord = 0, const CPVT_WordRange * pVisible = NULL);
	/**
	 * Generate a border appearance stream.
	 */
	static CFX_ByteString					GenerateBorderAP(const CPDF_Rect & rect, FX_FLOAT fWidth,
												const CPVT_Color & color, const CPVT_Color & crLeftTop, const CPVT_Color & crRightBottom,
												FX_INT32 nStyle, const CPVT_Dash & dash);
	/**
	 * Generate a color appearance stream.
	 */
	static CFX_ByteString					GenerateColorAP(const CPVT_Color & color,const FX_BOOL & bFillOrStroke);
};

#endif //_FPDF_AP_H_

/** @} */
